import request from '../../utils/request';

// IoT 产品信息接口
export interface ProductVO {
  id?: number;
  name: string;
  productKey?: string;
  categoryId?: number;
  categoryName?: string;
  description?: string;
  deviceType?: number;
  netType?: number;
  protocolType?: number;
  dataFormat?: number;
  deviceCount?: number;
  status?: number;
  validateType?: number;
  createTime?: Date;
  updateTime?: Date;
}

// 产品功能定义接口
export interface ProductFunctionVO {
  id?: number;
  productId: number;
  productKey?: string;
  identifier: string;
  name: string;
  description?: string;
  type: number; // 1=属性 2=服务 3=事件
  property?: string; // JSON格式的属性定义
  accessMode?: string; // 读写类型：r=只读，rw=读写
  required?: boolean;
  createTime?: Date;
}

// 产品分页查询参数
export interface ProductPageParams {
  pageNo?: number;
  pageSize?: number;
  name?: string;
  productKey?: string;
  categoryId?: number;
  deviceType?: number;
  status?: number;
}

// IoT 产品 API
export const ProductApi = {
  // 查询产品分页
  getProductPage: async (params: ProductPageParams) => {
    return await request.get('/iot/product/page', { params });
  },

  // 查询产品详情
  getProduct: async (id: number) => {
    return await request.get(`/iot/product/${id}`);
  },

  // 新增产品
  createProduct: async (data: ProductVO) => {
    return await request.post('/iot/product', data);
  },

  // 修改产品
  updateProduct: async (data: ProductVO) => {
    return await request.put('/iot/product', data);
  },

  // 删除产品
  deleteProduct: async (id: number) => {
    return await request.delete(`/iot/product/${id}`);
  },

  // 更新产品状态
  updateProductStatus: async (id: number, status: number) => {
    return await request.put(`/iot/product/${id}/status`, { status });
  },

  // 获取产品简单列表
  getProductSimpleList: async () => {
    return await request.get('/iot/product/simple-list');
  },

  // 导出产品
  exportProduct: async (params: ProductPageParams) => {
    return await request.get('/iot/product/export', { params, responseType: 'blob' });
  }
};

// 产品功能 API
export const ProductFunctionApi = {
  // 查询产品功能列表
  getProductFunctionList: async (productId: number, type?: number) => {
    return await request.get('/iot/product-function/list', { params: { productId, type } });
  },

  // 查询产品功能详情
  getProductFunction: async (id: number) => {
    return await request.get(`/iot/product-function/${id}`);
  },

  // 新增产品功能
  createProductFunction: async (data: ProductFunctionVO) => {
    return await request.post('/iot/product-function', data);
  },

  // 修改产品功能
  updateProductFunction: async (data: ProductFunctionVO) => {
    return await request.put('/iot/product-function', data);
  },

  // 删除产品功能
  deleteProductFunction: async (id: number) => {
    return await request.delete(`/iot/product-function/${id}`);
  }
};